package cn.iocoder.yudao.module.book.controller.app.collect;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import cn.iocoder.yudao.module.book.controller.app.collect.vo.AppCollectItemRespVO;
import cn.iocoder.yudao.module.book.dal.dataobject.collect.CollectDO;
import cn.iocoder.yudao.module.book.service.books.BooksService;
import cn.iocoder.yudao.module.book.service.collect.CollectService;
import cn.iocoder.yudao.module.book.service.collections.CollectionsService;
import cn.iocoder.yudao.module.book.service.users.userDTOService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 收藏")
@RestController
@RequestMapping("/api/user/collection")
public class AppCollectController {

    @Resource
    private CollectService collectService;

    @Resource
    private BooksService booksService;

    @Resource
    private userDTOService userService;

    @Resource
    private CollectionsService collectionsService;

    @GetMapping("/list")
    @Operation(summary = "获取收藏列表")
    public CommonResult<List<AppCollectItemRespVO>> list(@RequestParam(value = "type", defaultValue = "book") String type) {
        Long currentUserId = userService.getCurrentUserId();
        List<CollectDO> collectList = collectService.listCollectByType(currentUserId, type);
        List<AppCollectItemRespVO> result = new ArrayList<>(collectList.size());
        
        collectList.forEach(item -> {
            AppCollectItemRespVO collectItem = new AppCollectItemRespVO();
            collectItem.setCreateTime(item.getCreateTime() != null ? item.getCreateTime().toString() : null);
            
            if ("book".equals(type) && item.getBookId() != null && item.getBookId() > 0) {
                AppBooksRespVO book = booksService.getByBookId(item.getBookId(), null);
                if (book != null) {
                    collectItem.setType("book");
                    collectItem.setItemId(book.getBookId());
                    collectItem.setTitle(book.getTitle());
                    collectItem.setCover(book.getImage());
                    collectItem.setDescription("绘本作品"); // 绘本没有描述字段，使用默认描述
                    result.add(collectItem);
                }
            } else if ("collection".equals(type) && item.getCollectionId() != null && item.getCollectionId() > 0) {
                AppCollectionsRespVO collection = collectionsService.selectByLang(item.getCollectionId(), null);
                if (collection != null) {
                    collectItem.setType("collection");
                    collectItem.setItemId(collection.getCollectionId());
                    collectItem.setTitle(collection.getTitle());
                    collectItem.setCover(collection.getImage());
                    collectItem.setDescription(collection.getDescription());
                    collectItem.setBookCount(collection.getBookCnt());
                    collectItem.setIsEnglish(collection.getIsEnglish());
                    result.add(collectItem);
                }
            }
        });
        return success(result);
    }

    @GetMapping("/remove")
    @Operation(summary = "移除收藏")
    public CommonResult<Boolean> remove(@Valid @RequestParam("itemId") Integer itemId, 
                                       @RequestParam(value = "type", defaultValue = "book") String type) {
        Long currentUserId = userService.getCurrentUserId();
        if ("book".equals(type)) {
            return CommonResult.success(collectService.deleteCollectByBookId(itemId, currentUserId));
        } else if ("collection".equals(type)) {
            return CommonResult.success(collectService.deleteCollectByCollectionId(itemId, currentUserId));
        }
        return CommonResult.success(false);
    }

    @GetMapping("/collect")
    @Operation(summary = "收藏绘本")
    public CommonResult<Boolean> collect(@Valid @RequestParam("bookId") Integer bookId) {
        return CommonResult.success(collectService.collect(bookId, userService.getCurrentUserId()));
    }

    @GetMapping("/collect-collection")
    @Operation(summary = "收藏绘本集")
    public CommonResult<Boolean> collectCollection(@Valid @RequestParam("collectionId") Integer collectionId) {
        return CommonResult.success(collectService.collectCollection(collectionId, userService.getCurrentUserId()));
    }

    @GetMapping("/clean-duplicates")
    @Operation(summary = "清理重复收藏记录")
    public CommonResult<Integer> cleanDuplicates() {
        Long currentUserId = userService.getCurrentUserId();
        Integer cleanedCount = collectService.cleanDuplicateCollects(currentUserId);
        return CommonResult.success(cleanedCount);
    }
}
